package com.crawler.handler;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.crawler.dao.DButils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ContentHandler extends Thread{
	private final static String QUEUE_NAME = "book";
	private static Logger logger = LoggerFactory.getLogger(ContentHandler.class);
	private CountDownLatch count;  
	private int id;
	private String name;
	private String book_name;
	private String chapter;
	private String authors;
	private String  con_url;
	private String sort;
	
    public ContentHandler(int id, String chapter, String con_url,
			String sort) {
		this.id = id;
		this.chapter = chapter;
		this.con_url = con_url;
		this.sort = sort;
	}

	public ContentHandler(String name,String book_name, String authors, String chapter,
			String con_url, String sort) {
		this.authors = authors;
		this.chapter = chapter;
		this.con_url = con_url;
		this.sort = sort;
		this.book_name = book_name;
		this.name = name;
	}

	public void run() {  
        System.out.println(Thread.currentThread().getName() + "正在执行。。。"+"章节名称:"+chapter+"====url:"+con_url);  
        StringBuilder builder;
		try {
			builder = CrewlerHandler.getUrl(con_url);
			Pattern p = Pattern.compile("&nbsp;&nbsp;&nbsp;&nbsp;(.*?)<");
			Matcher m = p.matcher(builder.toString());
			StringBuilder connect = new StringBuilder();
			while (m.find()) {
				connect.append(m.group(1)+"\n");
			}
			Book book = new Book();
			book.setName(name);
			book.setBook_name(book_name);
			book.setAuther(authors);
			book.setTitle(chapter);
			book.setContent(connect.toString());
			send(book);
//			DButils.insertContent(id, chapter,connect.toString(), sort);
		}catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			count.countDown();  
		}
    }  
	
	public static void send(Book book){
		Connection connection = null;
		Channel channel = null;
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("127.0.0.1");
			factory.setPort(5672);
			factory.setUsername("admin");
			factory.setPassword("asdc123456");			
			connection = factory.newConnection();
			channel = connection.createChannel();
			
			channel.queueDeclare(QUEUE_NAME, false, false, false, null);
			String message = JSONObject.toJSONString(book);
			channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
			System.out.println(" [x] Sent '" +book.getName() + "'");			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}finally{
			try {
				channel.close();
				connection.close();			
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		
//		ContentHandler handler = new ContentHandler(1, "寸", "http://www.quanshuwang.com/book/135/135767/36148697.html", "36148697");
//		Thread t = new Thread(handler);
//		t.start();
		
//		try {
//			StringBuilder builder = CrewlerHandler.getUrl("http://www.quanshuwang.com/book/135/135767/36148697.html");
//			Pattern p = Pattern.compile("&nbsp;&nbsp;&nbsp;&nbsp;(.*?)<");
//			Matcher m = p.matcher(builder.toString());		
//			while (m.find()) {
//				System.out.println(m.group(1));
//			}			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}

	public CountDownLatch getCount() {
		return count;
	}

	public void setCount(CountDownLatch count) {
		this.count = count;
	}
}
